import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import java.text.*;

/**
 * Title:  Interrupt.java
 * Description:  Construct the interrupt controller frame
 * Copyright:    Copyright (c) 2010
 * @author WANG Jiechao and the Java Tutorial
 * @version 1.0
 */
public class Interrupt extends JPanel
                                    implements PropertyChangeListener {
    int num=10;
    //Labels to identify the fields    
    private JLabel[] label=new JLabel[num];

    //Strings for the labels
    private String[] labelString={"   Interrupt mask register (IMR) ",
    		"  Interrupt vector register (IVR) ",
    		" Interrupt priority register (IPR) ",
    		"Interrupt service register (ISR) ",
    		"Interrupt mask register ",
    		"Interrupt mask register ",
    		"Interrupt mask register ",
    		"Interrupt mask register ",
    		"Interrupt mask register ",
    		"Interrupt mask register "};

    //Fields for data entry
    private JFormattedTextField[] field=new JFormattedTextField[num];

    //Formats to format and parse numbers
    private NumberFormat[] format=new NumberFormat[num];

    public Interrupt() {
        super(new BorderLayout());
        JPanel labelPane = new JPanel(new GridLayout(0,1)); 
        JPanel fieldPane = new JPanel(new GridLayout(0,1));  
        
        //Create the labels.
        for (int i=0;i<num;i++){
        	format[i] = NumberFormat.getNumberInstance();
        	label[i]=new JLabel(labelString[i]);
        	//Create the text fields and set them up.
        	field[i]=new JFormattedTextField(format[i]);
        	field[i].setText(new String("0x00"));
        	field[i].setColumns(10);
        	field[i].addPropertyChangeListener("value", this);
        	//Tell accessibility tools about label/textfield pairs.
        	label[i].setLabelFor(field[i]);
        	//Lay out the labels in a panel.
        	labelPane.add(label[i]);
        	//Layout the text fields in a panel.
        	fieldPane.add(field[i]);
        }
        
        //Put the panels in this panel, labels on left,
        //text fields on right.
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        add(labelPane, BorderLayout.WEST);
        add(fieldPane, BorderLayout.CENTER);
    }

    /** Called when a field's "value" property changes. */
    public void propertyChange(PropertyChangeEvent e) {/*
        Object source = e.getSource();
        if (source == amountField) {
            amount = ((Number)amountField.getValue()).doubleValue();
        } else if (source == rateField) {
            rate = ((Number)rateField.getValue()).doubleValue();
        } else if (source == numPeriodsField) {
            numPeriods = ((Number)numPeriodsField.getValue()).intValue();
        }

        double payment = computePayment(amount, rate, numPeriods);
        paymentField.setValue(new Double(payment));
    */}

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("FormattedTextFieldDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new Interrupt();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    //Compute the monthly payment based on the loan amount,
    //APR, and length of loan.
    double computePayment(double loanAmt, double rate, int numPeriods) {
        double I, partial1, denominator, answer;

        numPeriods *= 12;        //get number of months
        if (rate > 0.01) {
            I = rate / 100.0 / 12.0;         //get monthly rate from annual
            partial1 = Math.pow((1 + I), (0.0 - numPeriods));
            denominator = (1 - partial1) / I;
        } else { //rate ~= 0
            denominator = numPeriods;
        }

        answer = (-1 * loanAmt) / denominator;
        return answer;
    }

    //Create and set up number formats. These objects also
    //parse numbers input by user.
    
}

